<?php
// +----------------------------------------------------------------------
// | @projectName  【order_system---点餐系统】
// +----------------------------------------------------------------------
// | @author        山西创客空间科技有限公司
// +----------------------------------------------------------------------
// | @date          2017年10月13日 星期五
// +----------------------------------------------------------------------
// | @Copyright     http://sx-ck.com All rights reserved.      
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\model\Common;
use app\common\Base;
use app\common\Error;
use think\Controller;
use think\Db;
use think\Request;

class Students extends Base 
{    
    private $sql = " SELECT a.*,CASE a.type WHEN 1 THEN '学生' WHEN 2 THEN '老师' ELSE '' END AS type_name,b.`name` AS class_name,c.card_code,
                    c.create_time,c.last_recharge_time,c.last_recharge_money,c.balance FROM students a LEFT JOIN class b ON a.class_id = b.id LEFT JOIN card c ON c.id = a.id ";
    // 渲染页面
    public function index ()
    {
        return $this -> fetch();
    }

    //查询数据
    public function jqgrid_all()
    {
    	$student = Db::name('students');
    	$result = $student -> query($this -> sql);
    	if ($result === false) {
    		return Base::echo_error(Error::DB_ERROR);
    	}
    	return $this -> echo_success($result);
    }

    // 获取班级
    public function get_class () 
    {
        $class = Db::name('class');
        $result = $class -> select();
        if ($result == false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        return $this -> echo_success($result);
    }

    // 检测班级
    protected function check_class_id ($class_id) 
    {
        $dbname = Db::name('class');
        if(Common::check_empty($class_id)) {
            return Base::echo_error(Error::CLASS_ID_IS_EMPTY);
        }
        $row = $dbname -> where("id",'=',"$class_id") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if(count($row) === 0) {
            return Base::echo_error(Error::CLASS_ID_NOT_EXIST);
        }
        return true;
    }
    //添加
    public function add() 
    {
        return $this -> add_or_update(true);
    }

    //修改
    public function update() 
    {
        return $this -> add_or_update(false);
    }
    //添加或更新
    protected function add_or_update($is_add)
    {
        $id = '';
        $students = Db::name('students');

        // 判断添加的学号是否合法
        $id = $this -> request -> post('id');        
        $dbname = Db::name('students');
        if(Common::check_empty($id) === true) {
            return Base::echo_error(Error::STUDENTS_ID_IS_EMPTY);
        }
        $row = $dbname -> where("id",'=',"$id") -> select();
        if($row === false) {
            return Base::echo_error(Error::DB_ERROR);
        }
        if (count($row) == 0) {
            if ($is_add) {
                $data['id'] = $id;
            } else {
                return Base::echo_error(Error::STUDENTS_ID_NOT_EXIST);
            }  
        }
        if(count($row) > 0) {
            if ($is_add) {
                return Base::echo_error(Error::STUDENTS_ID_ALREADY_EXIST);
            } else {
                $data['id'] = $id;
            }   
        }
        
        // 检测班级是否合法
        $class_id = $this -> request -> post('class_id');
        
        if($this -> check_class_id($class_id) === true) {
            $data['class_id'] = $class_id;
        } else {
            return $this -> check_class_id($class_id);
        }

        // 检测类型是否合法
        $type = $this -> request -> post('type');
        if (Common::check_empty($type) === true) {
            return Base::echo_error(Error::TYPE_IS_EMPTY);
        } else {
            $data['type'] = $type;
        }
        // 检测名字是否合法
        $name = $this -> request -> post('name');
        if (Common::check_empty($name) === true) {
            return Base::echo_error(Error::NAME_IS_EMPTY);
        } else {
            $data['name'] = $name;
        }
        // 新增数据
        if ($is_add === true) {
            if ($students -> insert($data) == 1) {
                return Base::echo_success(Error::ADD_SUCCESS);
            } else {
                return Base::echo_error(Error::ADD_ERROR);
            }
        }

        // 更新数据
        if ($is_add === false) {
            if ($students -> update($data) !== false) {
                return Base::echo_success(Error::UPDATE_SUCCESS);
            } else {
                return Base::echo_error(Error::UPDATE_ERROR);                
            }
        }
    }
    // 删除
    public function delete ()
    {
        $ids = $this -> request -> post('ids');
        if (Common::check_empty($ids)) {
            Base::echo_error(Error::ARGUMENT_ERROR);
        }
        $ids = substr($ids,0,-1);
        $idArr = explode(',',$ids);

        // 启动事务
        Db::startTrans();
        $students = Db::name('students');
        $card = Db::name('card');
        for ($i = 0; $i < count($idArr); $i++) { 
            $card_res = $card -> where('id',$idArr[$i]) -> delete();
            if ($card_res !== false) {
                $res = $students -> where('id',$idArr[$i]) -> delete();
                if (!$res) {
                    Db::rollback();
                    return Base::echo_error(Error::DELETE_ERROR);
                }
            }
            if ($card_res === false) {
                Db::rollback();
                return Base::echo_error(Error::DELETE_ERROR);
            }
        }
        // 提交事务            
        Db::commit();
        return Base::echo_success(Error::DELETE_SUCCESS);
    }
   

   
   

}
